//
//  HomeComView.m
//  Ginix
//
//  Created by 冯志强 on 2025/2/20.
//

#import "HomeComView.h"
#import "HomeComInfoCell.h"
#import "HomeComtypeCell.h"
#import "HomePulseCell.h"
#import "HomeTongueCell.h"
#import "HomeConstiCell.h"
#import "HomeComDataCell.h"
#import "HomeComTriggerView.h"
#import "HomeComNurseCell.h"
#import "HomeBtnCell.h"

@interface HomeComView()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView * listtView;
@property (nonatomic, copy) void(^scrollCallback)(UIScrollView *scrollView);
@property (nonatomic,strong) HomeAllCheckModel * infoModel;
@end

@implementation HomeComView

-(instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        [self setSubViewLayout];
        [self requestData];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getNotification:) name:RefshAllDataNotification object:nil];

    }
    return self;
}

-(void)setSubViewLayout{
    
    self.backgroundColor = UIColor.clearColor;
    [self addSubview:self.listtView];
    [_listtView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self).insets(UIEdgeInsetsMake(20 * FMScaleX, 0, 0, 0));
    }];
}

-(void)requestData{
    @WeakObj(self)
    [SPNetworkingIns POST:PORT(@"bh-center/api/invite/getUserHealthTestLatestNew") params:@{} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject, NSInteger code) {
        @StrongObj(self)
        self.infoModel = [HomeAllCheckModel yy_modelWithDictionary:responseObject[@"data"]];
        [self.listtView reloadData];
    } fail:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        
    }];
}

-(void)getNotification:(NSNotification *)notification{
    [self requestData];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 7;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
   
    switch (indexPath.row) {
        case 0:
            return  60 * FMScaleX;
            break;
        case 1:
            return  180 * FMScaleX;
            break;
        case 2:
            return  240 * FMScaleX;
            break;
        case 3:
            return  480 * FMScaleX;
            break;
        case 4:
            return  240 * FMScaleX;
            break;
        case 5:
            return  520 * FMScaleX;
            break;
        case 6:
            return  200 * FMScaleX;
            break;
        case 7:
            return  90 * FMScaleX;
            break;
        default:
            return 0;
            break;
    }}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        HomeComInfoCell * cell = [tableView dequeueReusableCellWithIdentifier:@"HomeComInfoCell"];
        [cell setInfoModel:self.infoModel type:0];
        return cell;
    }
    if(indexPath.row == 1){
        HomeComtypeCell * cell = [tableView dequeueReusableCellWithIdentifier:@"HomeComtypeCell"];
        [cell setInfoModel:self.infoModel];
        return cell;
    }
    if (indexPath.row == 2) {
        HomePulseCell * cell = [tableView dequeueReusableCellWithIdentifier:@"HomePulseCell"];
        [cell setInfoModel:self.infoModel.result.diagnosis];
        return cell;
    }
    if (indexPath.row == 3) {
        HomeTongueCell * cell = [tableView dequeueReusableCellWithIdentifier:@"HomeTongueCell"];
        [cell setInfoModel:self.infoModel.result.diagnosis];
        return cell;
    }
    if (indexPath.row == 4) {
        HomeComDataCell * cell = [tableView dequeueReusableCellWithIdentifier:@"HomeComDataCell"];
        [cell setInfoModel:self.infoModel.result.diagnosis];
        return cell;
    }
    if (indexPath.row == 5) {
        HomeComTriggerView * cell = [tableView dequeueReusableCellWithIdentifier:@"HomeComTriggerView"];
        [cell setInfoModel:self.infoModel.result.diagnosis];
        return cell;
    }
    HomeComNurseCell * cell = [tableView dequeueReusableCellWithIdentifier:@"HomeComNurseCell"];
    [cell setInfoModel:self.infoModel.result.diagnosis];
    return cell;
}


-(UITableView *)listtView{
    
    if (!_listtView) {
        _listtView = [[UITableView alloc]init];
        _listtView.delegate = self;
        _listtView.dataSource = self;
        _listtView.showsVerticalScrollIndicator = NO;
        _listtView.showsHorizontalScrollIndicator = NO;
        _listtView.backgroundColor = UIColor.clearColor;
        _listtView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _listtView.bounces = NO;
        [_listtView registerClass:[HomeComInfoCell class] forCellReuseIdentifier:@"HomeComInfoCell"];
        [_listtView registerClass:[HomeComtypeCell class] forCellReuseIdentifier:@"HomeComtypeCell"];
        [_listtView registerClass:[HomePulseCell class] forCellReuseIdentifier:@"HomePulseCell"];
        [_listtView registerClass:[HomeTongueCell class] forCellReuseIdentifier:@"HomeTongueCell"];
        [_listtView registerClass:[HomeConstiCell class] forCellReuseIdentifier:@"HomeConstiCell"];
        [_listtView registerClass:[HomeComDataCell class] forCellReuseIdentifier:@"HomeComDataCell"];
        [_listtView registerClass:[HomeComTriggerView class] forCellReuseIdentifier:@"HomeComTriggerView"];
        [_listtView registerClass:[HomeComNurseCell class] forCellReuseIdentifier:@"HomeComNurseCell"];
        [_listtView registerClass:[HomeBtnCell class] forCellReuseIdentifier:@"HomeBtnCell"];
    }
    return _listtView;
}

- (UIScrollView *)listScrollView {
    return self.listtView;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    self.scrollCallback(scrollView);
}

- (void)listViewDidScrollCallback:(void (^)(UIScrollView *))callback {
    self.scrollCallback = callback;
}

- (UIView *)listView {
    return self;
}
@end
